import os
import os.path as path
import effess
Import('build')
#Import('libkroll_sources')
#Import('libkroll_env')
#Import('libkroll_name')

libkroll_sources = []
libkroll_env = build.env.Clone()

# libkroll is called khost for now, until we can gaurantee
# that application updates also update the kboot.exe DLL.
libkroll_name = 'khost'

sources = libkroll_sources + \
	Glob('*.cpp') + \
	Glob('config/*.cpp') + \
	Glob('binding/*.cpp') + \
	Glob('utils/*.cpp') + \
	Glob('utils/poco/*.cpp') + \
	Glob('net/proxy_config.cpp') + \
	Glob('javascript/*.cpp') + \
	Glob('api/*.cpp') + \
	Glob('utils/%s/*.cpp' % build.os) + \
	Glob('net/*_%s.cpp' % build.os)

if build.is_linux():
	sources += Glob('linux/*.cpp') + Glob('utils/posix/*.cpp') 

if build.is_win32():
	sources += Glob('utils/unzip/*.cpp') + Glob('win32/*.cpp')

env = libkroll_env
env.Append(CPPDEFINES=[('KROLL_API_EXPORT', 1)])
build.add_thirdparty(env, 'poco')
build.add_thirdparty(env, 'webkit')
if build.is_osx():
	sources +=  Glob('osx/*.mm') + \
		Glob('utils/%s/*.mm' % build.os) + \
		Glob('utils/posix/*.cpp') + \
		Glob('net/*_%s.mm' % build.os)

if build.is_linux():
	if os.environ.has_key('PKG_CONFIG_PATH'):
		env.AppendENVPath('PKG_CONFIG_PATH', os.environ['PKG_CONFIG_PATH'])
	env.ParseConfig('pkg-config --cflags --libs libxml-2.0 gtk+-2.0 gdk-2.0 glib-2.0 gthread-2.0')
	env.Append(LIBS=['pthread', 'libsoup-2.4', 'libproxy', 'libgcrypt', 'libgnutls'])

if build.is_win32():
	env.Append(CPPPATH=[path.join(build.third_party, 'icu', 'include')])
	env.Append(LIBS=[
		'Advapi32', 'comctl32', 'icuuc',
		'icuin', 'iphlpapi', 'kernel32',
		'ole32', 'oleaut32', 'pthreadVC2',
		'shell32', 'shlwapi', 'user32',
		'winhttp',
	])
	env.Append(CCFLAGS=['/DUNICODE', '/D_UNICODE'])
	env.Append(LINKFLAGS=['/LTCG', '/INCREMENTAL:NO'])

elif build.is_osx():
	env.Append(LINKFLAGS='-install_name lib' + libkroll_name + '.dylib')
	env.Append(LIBS=['ssl', 'crypto'])
	env.Append(FRAMEWORKS=['Cocoa', 'SystemConfiguration', 'CoreServices'])

build.mark_build_target(
	env.SharedLibrary(path.join(build.dir, 'runtime', libkroll_name), source=sources))
build.utils.LightWeightCopy(path.join(build.cwd(), 'support'), path.join(build.dir, 'runtime'))

# copy the includes for building the SDK
kroll_include = path.join(build.kroll_include_dir, 'kroll')
effess.copy_tree(build.cwd(), kroll_include, include=['.h'])
